<?php

namespace App\Http\Controllers\Resource;

use Illuminate\Support\Facades\Log;

/**
 * QQ阅读 数字资源
 *
 *  未提供目录接口
 */
class QqrdController extends CommonController
{
    public $appflag = 'yzqlib';
    public $secret = '238a474b57deb2bdf7206de1389239ee';


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 图书检索接口
     *          基础参数
     * @param  keyword String 否 登高望远 检索关键词     不参与签名
     * @param  page int 是 1 1 当前页
     *
     */
    public function bookSearch($keywords = null, $page = 1)
    {
        $url = 'http://www.readse.com/qqreader/open/api/full/book.search.action';
        $param = [
            'appflag' => $this->appflag,
            'timestamp' => time(),
            'keywords' => $keywords,
            'page' => $page,
        ];
        $sign = $this->getSingInfo($param);
        $params = http_build_query($param);
        $url .= "?" . $params . '&sign=' . $sign;
        $result = request_url($url);
        $result = json_decode($result, true);

        if (empty($result['result']) || $result['result'] !== 'success') {
            Log::channel('qqrdlog')->debug('资源获取失败');
            Log::channel('qqrdlog')->debug($param);
            Log::channel('qqrdlog')->debug($result);
            Log::channel('qqrdlog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }



    /**
     * 获取图书基本信息
     * @param  bid String 书籍唯一标识
     */
    public function getBookDetail($bid)
    {
        $url = 'http://www.readse.com/qqreader/open/api/full/book.info.action';
        $param = [
            'appflag' => $this->appflag,
            'timestamp' => time(),
            'bid' => $bid
        ];
        $sign = $this->getSingInfo($param);
        $params = http_build_query($param);
        $url .= "?" . $params . '&sign=' . $sign;
        $result = request_url($url);
        $result = json_decode($result, true);

        if (empty($result['result']) || $result['result'] !== 'success') {
            Log::channel('qqrdlog')->debug('资源获取失败');
            Log::channel('qqrdlog')->debug($param);
            Log::channel('qqrdlog')->debug($result);
            Log::channel('qqrdlog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }



    /**
     * 获得图书分类
     * @param  bid String 书籍唯一标识
     */
    public function getBookType()
    {
        $url = 'http://www.readse.com/qqreader/open/api/full/book.categorys.action';
        $param = [
            'appflag' => $this->appflag,
            'timestamp' => time()
        ];
        $sign = $this->getSingInfo($param);
        $params = http_build_query($param);
        $url .= "?" . $params . '&sign=' . $sign;
        $result = request_url($url);
        $result = json_decode($result, true);

        if (empty($result['result']) || $result['result'] !== 'success') {
            Log::channel('qqrdlog')->debug('资源获取失败');
            Log::channel('qqrdlog')->debug($param);
            Log::channel('qqrdlog')->debug($result);
            Log::channel('qqrdlog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }


    /**
     * 根据图书分类获取书籍列表
     * @param   category 分类标识
     * @param   page 页码，最小值为 1
     */
    public function qqrdTypeBookList($type_id, $page = 1)
    {
        $url = 'http://www.readse.com/qqreader/open/api/full/book.category.list.action';
        $param = [
            'appflag' => $this->appflag,
            'timestamp' => time(),
            'category' => $type_id,
            'page' => $page,
        ];
        $sign = $this->getSingInfo($param);
        $params = http_build_query($param);
        $url .= "?" . $params . '&sign=' . $sign;
        $result = request_url($url);
        $result = json_decode($result, true);

        if (empty($result['result']) || $result['result'] !== 'success') {
            Log::channel('qqrdlog')->debug('资源获取失败');
            Log::channel('qqrdlog')->debug($param);
            Log::channel('qqrdlog')->debug($result);
            Log::channel('qqrdlog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }


    /**
     * 获得所有已上架图书标识
     * @param   page 页码，最小值为 1
     */
    public function qqrdUpBookList($page = 1)
    {
        $url = 'http://www.readse.com/qqreader/open/api/full/id.list.action';
        $param = [
            'appflag' => $this->appflag,
            'timestamp' => time(),
            'page' => $page,
        ];
        $sign = $this->getSingInfo($param);
        $params = http_build_query($param);
        $url .= "?" . $params . '&sign=' . $sign;
        $result = request_url($url);
        $result = json_decode($result, true);
        if (empty($result['result']) || $result['result'] !== 'success') {
            Log::channel('qqrdlog')->debug('资源获取失败');
            Log::channel('qqrdlog')->debug($param);
            Log::channel('qqrdlog')->debug($result);
            Log::channel('qqrdlog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }

    /**
     * 获得所有已下架图书标识
     * @param   category 分类标识
     * @param   page 页码，最小值为 1
     */
    public function qqrdDownBookList($page = 1)
    {
        $url = 'http://www.readse.com/qqreader/open/api/full/id.removed.action';
        $param = [
            'appflag' => $this->appflag,
            'timestamp' => time(),
            'page' => $page,
        ];
        $sign = $this->getSingInfo($param);
        $params = http_build_query($param);
        $url .= "?" . $params . '&sign=' . $sign;
        $result = request_url($url);
        $result = json_decode($result, true);
        if (empty($result['result']) || $result['result'] !== 'success') {
            Log::channel('qqrdlog')->debug('资源获取失败');
            Log::channel('qqrdlog')->debug($param);
            Log::channel('qqrdlog')->debug($result);
            Log::channel('qqrdlog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }


    /**
     * 获取签名信息
     * @param param 所有签名参数
     */
    public function getSingInfo($param)
    {
        $param = $this->arraySort($param);
        $params = $this->secret; //将密钥凭拼接在字符串的首部
        foreach ($param as $key => $val) {
            $params .= $key;
            if (!contains_chinese($val)) {
                $params .= $val; //包含中文不参加签名
            }
        }
        return  strtoupper(md5($params));
    }


    /**
     * 字典序排序
     */
    public function arraySort(array $param)
    {
        ksort($param);
        reset($param);

        return $param;
    }
}
